<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Container extends Model
{

    function getContainersContent($where, $type, $lang, $port_id = 0, $page = 1, $pageSize = 10000000)
    {
        // 获取准备状态货柜
        $container = getDatas('container', ['id,name,ready_day,sum_ctn,sum_kgs,sum_cbm,sum_value,create_id,create_at'], ['etd_day' => 0], 'ready_day asc');
        // if (!empty($container)) {
        foreach ($container as $k => $v) {
            $container[$k]['create_name'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者
            $container[$k]['create_name'] = getTimeDates($v['create_at'], $lang);
            $container[$k]['ready_time'] = getTimeDates($v['ready_day'], $lang, true);
        }

        if ($type == 'add') {

            if ($port_id) {
                $where['port_id'] = $port_id;
            }
            // 获取合同
            $buy_po = model('Buy')->getBuyPo($where, $lang, ['zu.id,zu.company_id,zu.delivery_day,zu.paid_sum,zu.price_sum,zu.cost_sum']);
            $where['container_id'] = 0;
        } else {
            // 如果不传 则获取第一个货柜的装箱清单数据
            if (empty($where)) {
                $where['container_id'] = isset($container[0]['id']) ? $container[0]['id'] : 0;
            }
            // 获取合同Id
            $po_ids = getGroup('buy_po_pack', ['po_id'], $where, 'po_id');
            if (!empty($po_ids)) {
                $res = array_column($po_ids, 'po_id');
                $po_ids = implode($res, ',');
            }
            $wherepo_id = ['zu.id' => ['in', $po_ids]];

            $buy_po = model('Buy')->getBuyPo($wherepo_id, $lang, ['zu.id,zu.company_id,zu.delivery_day,zu.paid_sum,zu.price_sum,zu.cost_sum']);
        }
        $buy_po_pack = [];
        $footer = [];
        $packNum = 0;
        $where2['container_id'] = $where['container_id'];
        if (!empty($buy_po)) {
            $buy_po_pack = model('Buy')->getPoPack($buy_po, $where2, $lang, 1, 1000000);
            if (!empty($buy_po_pack)) {
                $footer['num'] = 0;
                $footer['sum_CTN'] = 0;
                $footer['sum_KGS'] = 0;
                $footer['sum_CBM'] = 0;
                $footer['sum_CNY'] = 0;
                foreach ($buy_po_pack as $k1 => $v1) {
                    $packNum += count($v1['buy_po_pack']);
                }
                $result = $this->getPagination($buy_po_pack,$pageSize,$page,$footer);

            }
        }
        $returnData['container'] = $container;
        $returnData['buy_po_pack']['content'] = isset($result)  ? $result['buy_po_pack'] : [];
        $returnData['buy_po_pack']['footer'] = isset($result)  ? $result['footer'] : [];
        $returnData['buy_po_pack']['packNum'] = $packNum;
        $returnData['buy_po_pack']['page'] = $page;
        return $returnData;
    }


    // 分页获取数据
    function getPagination($data,$pageSize,$page,$footer)
    {
        $i = 0;
        $arr = [];
        foreach ($data as $k1 =>$v1){
            if($i < $pageSize*$page){
                $arr[$k1] = $v1;
                unset($arr[$k1]['buy_po_pack']);
                foreach ($v1['buy_po_pack'] as $key => $value) {
                    if($i < $pageSize*$page && $i >= ($page-1)*$pageSize){
                        if($i < $pageSize*$page){
                            $arr[$k1]['buy_po_pack'][] = $value;

                            $footer['num']++;
                            $footer['sum_CTN'] += $value['qty'];
                            $footer['sum_CNY'] += $value['sum_price'];
                            $footer['sum_KGS'] += $value['sum_kgs'];
                            $footer['sum_CBM'] += $value['sum_m3']; 
                        }
                    }
                    $i++;
                }   
            }
            if(!isset($arr[$k1]['buy_po_pack'])) unset($arr[$k1]);
            
        }
        $arr = array_values($arr);
        $returnData['buy_po_pack'] = $arr;
        $returnData['footer'] = $footer;
        return $returnData;
    }

    //货柜列表 货柜抵达
    public function getcontainer($res)
    {
        $map = array();
        if (isset($res['status']) && $res['status'] == 1) {
            //准备
            $map['ready_day'] = array('gt', 0);//准备时间
            $map['arrival_day'] =$map['eta_day'] =$map['etd_day'] = $map['sum_ctn'] = 0;//未入仓 未到达 未开航 无货值
        } elseif (isset($res['status']) && $res['status'] == 2) {
            //装柜
            $map['sum_ctn'] = $map['ready_day'] = array('gt', 0);//含有货值  已准备
            $map['arrival_day'] = $map['eta_day']=$map['etd_day'] = 0;//未入仓 未到达 未开航
        } elseif (isset($res['status']) && $res['status'] == 3) {
            //出发
            $map['etd_day'] = array('gt', 0);//已开航
            $map['arrival_day'] =$map['eta_day'] = 0;//未入仓 未到达
        } elseif (isset($res['status']) && $res['status'] == 4) {
            //到达
            $map['eta_day'] = array('gt', 0);//已到达
            $map['arrival_day'] = 0;//未入仓
        } elseif (isset($res['status']) && $res['status'] == 5) {
            //完成
            $map['arrival_day'] = array('gt', 0);//已入仓
        }
        $admin=session('admin');
        if($admin['position_id']==65){
            //货柜监装 查看90 天以内的货柜 （已开航未到达）
            $ningthday = mktime(0, 0, 0, date('m'), date('d') - 90, date('Y')) - 1;
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $map['etd_day'] = ['between', "$ningthday,$endToday"];//查看的90天内的开航货柜
            $map['eta_day']=0;//未到达
        }
        $data = Db::name('container')->where($map)->page($res['page'], $res['pageSize'])->order('update_at desc')->select();
        $lang = $res['lang'];
        foreach ($data as $datakey => $dataval) {
            //出发地目的地
            $data[$datakey]['port_start_name'] = getValue('port', ['id' => $dataval['port_start']], 'name');
            $data[$datakey]['port_arrival_name'] = getValue('port', ['id' => $dataval['port_arrival']], 'name');
            //设置状态
            if ($dataval['arrival_day'] > 0) {
                //完成 （已入仓）
                $data[$datakey]['status'] = 'DISCHARGE';
            } elseif ($dataval['eta_day'] > 0 && $dataval['arrival_day']==0) {
                //到达  （取消到港、入仓） 已到达 未入仓
                $data[$datakey]['status'] = 'ARRIVE';
            } elseif ($dataval['etd_day'] > 0 && $dataval['eta_day']==0 && $dataval['arrival_day']==0) {
                //出发 （到港、取消开航） 已开航 未到达 未入仓
                $data[$datakey]['status'] = 'SALLING';
            } elseif ($dataval['ready_day'] > 0 && $dataval['sum_ctn'] > 0 && $dataval['etd_day']==0 && $dataval['eta_day']==0 && $dataval['arrival_day']==0) {
                //装柜 （开航）  已准备  有货值 未开航  未到达 未入仓
                $data[$datakey]['status'] = 'LOADING';
            } elseif ($dataval['ready_day'] > 0 && $dataval['sum_ctn'] == 0 && $dataval['etd_day']==0 && $dataval['eta_day']==0 && $dataval['arrival_day']==0) {
                //准备 （删除）  已准备  没有货值 未开航 未到达 未入仓
                $data[$datakey]['status'] = 'READY';
            }
            //入仓日期
            if ($dataval['arrival_day']>0) {
                $data[$datakey]['status_time'] = getTimeDate($dataval['arrival_day'], 7);
            }
            //承运商名称
            $containerwhere['mkey'] = 'container_carrier_' . $dataval['carrier_id'];
            $data[$datakey]['carriername'] = getlangname('multi_lang', $containerwhere, $lang);
            //编辑者编辑时间
            $data[$datakey]['mangname'] = getinfo($dataval['update_id'], 'admin')['nickname'];//编辑者
            $data[$datakey]['update_date'] = getTimeDate($dataval['update_at'], 12);
            //货柜类型名称
            $data[$datakey]['container_type_name'] = getinfo($dataval['type_id'], 'container_type')['name'];
            $data[$datakey]['agent_start_name'] = getValue('container_agent', ['id' => $dataval['co_id_start']], 'name');  //出发货贷
            $data[$datakey]['agent_arrival_name'] = getValue('container_agent', ['id' => $dataval['co_id_arrival']], 'name');//到达贷贷
            //处理货柜的准备时间
            $data[$datakey]['ready_date'] = getTimeDate($dataval['ready_day'], 10);
        }
        $datares['data'] = $data;
        $datares['size'] = Db::name('container')->where($map)->count();
        $datares['ids'] = array_column($data, 'id');//货柜id
        $datares['ready_date'] = array_values(array_unique(array_column($data, 'ready_date')));//准备时间
        $datares['langs'] = getlangnames();
        return $datares;
    }

    //货柜的添加
    public function containeradd($data)
    {
        $ckdata = notEmpty(['name' => lang("container_name"), 'ready_day' => lang("departure_time"), 'number' => lang("container_quantity")]);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        $namearr = array();
        //计算加密的密文
        for ($i = 1; $i <= $data['number']; $i++) {
            if ($data['number'] == 1) {
                $rowname = $data['name'];//货柜名称
            } else {
                $rowname = $data['name'] . '(' . $i . ')';//货柜名称
            }
            $row = [
                'name' => $rowname,//货柜名称
                'ready_day' => strtotime($data['ready_day']),//准备时间
                'type_id' => $data['type_id'],//货柜类型
                'port_start' => $data['port_start'],//出发港
                'port_arrival' => $data['port_arrival'],//到达港
                'co_id_start' => $data['co_id_start'],//出发货贷
                'co_id_arrival' => $data['co_id_arrival'],//到达货贷
                'create_id' => session('userid'),
                'update_id' => session('userid'),
                'create_at' => time(),
                'update_at' => time(),
            ];
            array_push($namearr, $rowname);
            $ids[] = $this->insertGetId($row);
        }
        foreach ($ids as $idskey => $idsval) {
            $rangnum = mt_rand(1, 9);//每次id改变的时候随机数不一样
            if (strlen($rangnum) > 1) {
                $rangnum = substr($rangnum, 5, 1);
            }
            $num = substr($idsval, -3);//取后三位数
            //不足三位数
            if (strlen($num) == 1) {
                $num = '00' . $num;
            } elseif (strlen($num) == 2) {
                $num = '0' . $num;
            }
            $arr = str_split($num, 1);//转换成数组
            $one = $rangnum * $arr[0];
            //第一位数
            if ($one == 0) {
                $numone = '00';
            } else {
                if ($one <= 9) {
                    $numone = '0' . $one;
                } else {
                    $numone = $one;
                }
            }
            //第二位数
            $two = $rangnum * $arr[1];
            if ($two == 0) {
                $numtwo = '00';
            } else {
                if ($two <= 9) {
                    //含有一位数
                    $numtwo = '0' . $two;
                } else {
                    //含有两位数
                    $numtwo = $two;
                }
            }
            //第三位数
            $three = $rangnum * $arr[2];
            if ($three == 0) {
                $numthree = '00';
            } else {
                if ($three <= 9) {
                    //含有一位数
                    $numthree = '0' . $three;
                } else {
                    //含有两位数
                    $numthree = $three;
                }
            }
            $code = $numone . mt_rand(1, 9) . $numtwo . $rangnum . $numthree . mt_rand(1, 9);
            //更新密文
            $updateid = $this->where('id', $idsval)->setField('code', $code);
        }
        $namestr = implode(",", $namearr);
        if ($ids) {
            return array('msg' => $namestr . lang("added_successfully"), 'status' => 1);
        } else {
            return array('msg' => $namestr . lang("add_failed"), 'status' => 0);
        }
    }

    //编辑货柜显示的接口
    public function editinfo($data, $lang)
    {
        $where['a.lang'] = $lang;//语言标志
        $join = [
            ['nt_container_carrier b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        $info = getinfo($data['id'], 'container');
        $info['update_name'] = getinfo($info['update_id'], 'admin')['nickname'];//编辑者
        $info['update_date'] = getTimeDate($info['update_at'], 12);//编辑时间
        $containerwhere['mkey'] = 'container_carrier_' . $info['carrier_id'];
        $info['carrier_id_name'] = getlangname('multi_lang', $containerwhere, $lang);//承运商名称
        //状态
        if ($info['ready_day'] > 0 && $info['sum_ctn'] == 0 && $info['etd_day']==0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //准备   已准备  没有货值  未开航 未到达 未入仓
            $info['status'] = 'READY';
            $info['num'] = 1;
        } elseif ($info['ready_day'] > 0 && $info['sum_ctn'] > 0 && $info['etd_day']==0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //装柜   已准备  有货值  未开航 未到达 未入仓
            $info['status'] = 'LOADING';
            $info['num'] = 2;
        } elseif ($info['etd_day'] > 0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //出发   已开航 未到达 未入仓
            $info['status'] = 'SALLING';
            $info['num'] = 3;
        } elseif ($info['eta_day'] > 0 && $info['arrival_day']==0) {
            //到达  已到达 未入仓
            $info['status'] = 'ARRIVE';
            $info['num'] = 4;
        } elseif ($info['arrival_day'] > 0) {
            //卸货  已入仓
            $info['status'] = 'DISCHARGE';
            $info['num'] = 5;
        }
        //处理时间
        if ($info['ready_day']>0) {
            //准备时间
            $info['ready_day'] = getTimeDate($info['ready_day'], 7);
        }
        if ($info['etd_day']>0) {
            //开航时间
            $info['etd_day'] = getTimeDate($info['etd_day'], 7);
        }
        if ($info['eta_day']>0) {
            //到达时间
            $info['eta_day'] = getTimeDate($info['eta_day'], 7);
        }
        $datares = array(
            'container_type' => getables('container_type', [], 'id,name'),//货柜类型
            'port_start' => getables('port', ['country_id' => 2], 'id,name'),//出发地
            'port_arrival' => getables('port', ['country_id' => 1], 'id,name'), //目标地
            'co_id_start' => getables('container_agent', ['country_id' => 2], 'id,name'),// 货贷(中国)
            'co_id_arrival' => getables('container_agent', ['country_id' => 1], 'id,name'),  //货贷(巴西)
            'carrier' => getmultidata($table, $join, $where)['data'],//承运商
            'info' => $info,
        );
        return $datares;
    }

    public function containeredit($data)
    {
        $ckdata = notEmpty(['name' => lang("container_name"), 'ready_day' => lang("departure_time")]);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        $update=$data['data'];
        //处理时间
        if ($update['ready_day']>0) {
            //准备时间
            $update['ready_day'] = strtotime($update['ready_day']);
        }
        if ($update['etd_day']>0) {
            //开航时间
            $update['etd_day'] = strtotime($update['etd_day']);
        }
        if ($update['eta_day']>0) {
            //到达时间
            $update['eta_day'] = strtotime($update['eta_day']);
        }
        $update['update_id'] = session('userid');
        $update['update_at'] = time();
        //对数据的更新的操作
        $updateid = $this->where('id', $update['id'])->update($update);
        if ($updateid !== false) {
            return array('msg' => $update['name'] . lang("edited_successfully"), 'status' => 1);
        } else {
            return array('msg' => $update['name'] . lang("edit_failed"), 'status' => 0);
        }
    }

    //批量调整的显示接口
    public function getchange($data, $lang)
    {
        $map['id'] = ['in', $data['ids']];
        $datares = getables('container', $map, 'id,name,ready_day,etd_day,eta_day,arrival_day');
        foreach ($datares as $datareskey => $dataresval) {
            if ($dataresval['ready_day']>0) {
                $datares[$datareskey]['ready_day'] = getTimeDate($dataresval['ready_day'], 7);
            }
            if ($dataresval['etd_day']>0) {
                $datares[$datareskey]['etd_day'] = getTimeDate($dataresval['etd_day'], 7);
            }
            if ($dataresval['eta_day']>0) {
                $datares[$datareskey]['eta_day'] = getTimeDate($dataresval['eta_day'], 7);
            }
            if ($dataresval['arrival_day']>0) {
                $datares[$datareskey]['arrival_day'] = getTimeDate($dataresval['arrival_day'], 7);
            }
            $datares[$datareskey]['id']=numzero($dataresval['id']);
        }
        $name = implode(',', array_column($datares, 'name'));
        $changedata = array('name' => $name, 'data' => $datares);
        return $changedata;
    }

    //批量调整的提交接口
    public function containerchang($data)
    {
        if (!$data['time']) {
            return array('msg' => lang("please_set_the_time"), 'status' => 0);
        }
        $ids = $data['id'];
        $arrname = explode(',', $data['name']);//货柜名称
        if (count($arrname) > 3) {
            $namecontainer = $arrname[0] . $arrname[1] . $arrname[2] . '...';
        } else {
            $namecontainer = $data['name'];
        }
        $time = strtotime($data['time']);
        $update['update_id'] = session('userid');
        $update['update_at'] = time();
        if ($data['num'] == 1) {
            //准备日期
            $update['ready_day'] = $time;
        } elseif ($data['num'] == 2) {
            //开航日期
            $update['etd_day'] = $time;
        } elseif ($data['num'] == 3) {
            //到港日期
            $update['eta_day'] = $time;
        } elseif ($data['num'] == 4) {
            //入仓日期
            $update['arrival_day'] = $time;
        }
        $updateid = $this->where("id in($ids)")->update($update);
        if ($updateid !== false) {
            return array('msg' => $namecontainer . lang("edited_successfully"), 'status' => 1);
        } else {
            return array('msg' => $namecontainer . lang("edit_failed"), 'status' => 0);
        }
    }

    //开航 到达 接口
    public function etdetaarrivalday($data)
    {
        $time = $update['update_at'] = time();
        $update['update_id'] = session('userid');
        $name = trim($data['name']);
        if ($data['num'] == 1) {
            //开航
            $update['etd_day'] = $time;
            $yes = $name . lang("sailed_successfully");
            $no = $name . lang("sailing_failed");
        } elseif ($data['num'] == 11) {
            //取消开航
            $update['etd_day'] = 0;
            $yes = $name . lang("cancel_sailed_successfully");
            $no = $name . lang("cancel_sailing_failed");
        } elseif ($data['num'] == 2) {
            //到港
            $update['eta_day'] = $time;
            $yes = $name . lang("successful_arrival");
            $no = $name . lang("arrival_failure");
        } elseif ($data['num'] == 22) {
            //取消到港
            $update['eta_day'] = 0;
            $yes = $name . lang("cancel_successful_arrival");
            $no = $name . lang("cancel_arrival_failure");
        }
        $updateid = $this->where('id', $data['id'])->update($update);
        if ($updateid !== false) {
            return array('msg' => $yes, 'status' => 1);
        } else {
            return array('msg' => $no, 'status' => 0);
        }
    }

    //货柜删除接口
    public function delcontainer($data)
    {
        $sum_ctninfo = $this->where('id', $data['id'])->find();
        if ($sum_ctninfo['sum_ctn'] > 0) {
            return array('msg' => $data['name'] . lang("contains_total_number_of_boxes_and_can't_be_deleted"), 'status' => 0);
        }
        $buy_poin = Db::name('buy_po_pack')->where('container_id', $data['id'])->find();
        if ($buy_poin) {
            return array('msg' => $data['name'] . lang("contains_product_and_can't_be_deleted"), 'status' => 0);
        }
        $retcontainer = $this->where('id', $data['id'])->delete();//货柜表
        if ($retcontainer) {
            return array('msg' => $data['name'] . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $data['name'] . lang("failed_to_delete"), 'status' => 0);
        }
    }

    //详情信息的展示
    public function getdetail($data, $lang)
    {
        $truedata = $this->getNotStockedData($data['id'], $lang, null, null, 'false');//货柜中的产品
        $falsedata = $this->getNotStockedData($data['id'], $lang, null, null, 'true');//货柜中未入仓产品
        $info = Db::name('container')->where('id', $data['id'])->find();
        //货柜类型
        $info['type_name'] = getinfo($info['type_id'], 'container_type')['name'];
        //出发地
        $info['port_start_name'] = getinfo($info['port_start'], 'port')['name'];
        //出发地货贷
        $info['co_id_start_name'] = getinfo($info['co_id_start'], 'container_agent')['name'];
        //目的地
        $info['port_arrival_name'] = getinfo($info['port_arrival'], 'port')['name'];
        //目的地货贷
        $info['co_id_arrival_name'] = getinfo($info['co_id_arrival'], 'container_agent')['name'];
        //承运商
        $containerwhere['mkey'] = 'container_carrier_' . $info['carrier_id'];
        $info['carrier_id_name'] = getlangname('multi_lang', $containerwhere, $lang);
        //准备时间
        if ($info['ready_day']>0) {
            $info['ready_day'] = getTimeDate($info['ready_day'], 7);
        }
        //开航时间
        if ($info['etd_day']>0) {
            $info['etd_day'] = getTimeDate($info['etd_day'], 7);
        }
        //到达时间
        if ($info['eta_day']>0) {
            $info['eta_day'] = getTimeDate($info['eta_day'], 7);
        }
        //入仓日期
        if ($info['arrival_day']>0) {
            $info['arrival_day'] = getTimeDate($info['arrival_day'], 7);
        }
        if ($info['ready_day'] > 0 && $info['sum_ctn'] == 0 && $info['etd_day']==0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //准备
            $info['status'] = 'READY';
        } elseif ($info['ready_day'] > 0 && $info['sum_ctn'] > 0 && $info['etd_day']==0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //装柜
            $info['status'] = 'LOADING';
        } elseif ($info['etd_day'] > 0 && $info['eta_day']==0 && $info['arrival_day']==0) {
            //出发
            $info['status'] = 'SALLING';
        } elseif ($info['eta_day'] > 0 && $info['arrival_day']==0) {
            //到达
            $info['status'] = 'ARRIVE';
        } elseif ($info['arrival_day'] > 0) {
            //卸货
            $info['status'] = 'DISCHARGE';
        }
        //显示所有的仓库
        $warehouse = getables('warehouse', null, 'id,name');
        $langs = getlangnames();
        return array(
            'left' => $info,
            'trueright' => $truedata,
            'falsedata' => $falsedata,
            'warehouse' => $warehouse,
            'langs' => $langs
        );
    }

    //处理打印的详情接口
    public function getcontainerdayi($data)
    {
        $lang = $data['langname'];
        $returnData = $this->getNotStockedData($data['container_id'], $lang, null, null, 'false');
        $info = Db::name('container')->where('id', $data['container_id'])->find();
        //承运商名称
        $containerwhere['mkey'] = 'container_carrier_' . $info['carrier_id'];
        $info['carriername']=getlangname('multi_lang', $containerwhere, $lang);
        //登录者
        $info['mangname'] = session('admin')['nickname'];
        $info['mangtime'] = getTimeDate(time(), 9);
        $info['format'] = $data['format'];
        //准备时间
        if ($info['ready_day']>0) {
            $info['ready_day'] = getTimeDate($info['ready_day'], 7);
        }
        //开航时间
        if ($info['etd_day']>0) {
            $info['etd_day'] = getTimeDate($info['etd_day'], 7);
        }
        //到达时间
        if ($info['eta_day']>0) {
            $info['eta_day'] = getTimeDate($info['eta_day'], 7);
        }
        //入仓日期
        if ($info['arrival_day']>0) {
            $info['arrival_day'] = getTimeDate($info['arrival_day'], 7);
        }
        return array('info' => $info, 'data' => $returnData);
    }


    // 根据货柜ID(container_id)，获取未入库产品信息
    protected function getNotStockedData($id, $lang, $page = null, $pagesize = null, $issale_qty = 'false')
    {
        $map['container_id'] = $id;
        if ($issale_qty == 'true') {
            $map['warehouse_id'] = 0; //还没入仓的产品
            $po_ids = array_unique(array_column(getables('buy_po_pack', $map), 'po_id'));
        } else {
            //全部产品id
            $po_ids = array_unique(array_column(getables('buy_po_pack', $map), 'po_id'));
        }
        $returnData = [];
        foreach ($po_ids as $k => $v) {
            $where['zu.id'] = $v;
            $companyInfo = getSearch('buy_po', 'zu', ['zu.id,zu.company_id,zu.port_id,zu.receipt_day,cm.name company_name,port.name port_name'], $where, [['company cm', 'zu.company_id = cm.id'], ['port', 'zu.port_id = port.id']], 'zu.id desc');
            $aa = model('Buy')->getPoPack($companyInfo, ['po_id' => $v, 'container_id' => $id], $lang, $page, $pagesize, $issale_qty);
            if (!empty($aa)) {
                foreach ($aa as $k => $v) {
                    foreach ($v['buy_po_pack'] as $k1 => $v1) {
                        $aa[$k]['buy_po_pack'][$k1]['warehouse_name'] = $v1['warehouse_id'] ? getValue('warehouse', ['id' => $v1['warehouse_id']], 'name') : '';
                        $aa[$k]['buy_po_pack'][$k1]['warehouse_create'] = $v1['warehouse_id'] ? getTimeDates(getValue('warehouse', ['id' => $v1['warehouse_id']], 'create_at'), $lang) : '';
                        $aa[$k]['buy_po_pack'][$k1]['warehouse_placeName'] = $v1['place_id'] ? getValue('warehouse_place', ['id' => $v1['place_id']], 'name') : '';
                        $cifmap['pro_id']=$v1['pro_id'];
                        $cifmap['prochar_id']=$v1['prochar_id'];
                        $aa[$k]['buy_po_pack'][$k1]['cif']=getValue('pro_sale',$cifmap,'cif');
                    }
                }
                $returnData[] = $aa['0'];
            }
        }
        return $returnData;
    }

    //详情信息入库的操作
    public function setstrong($data)
    {
        if (!isset($data['prolist'])) {
            return array('msg' => '请先勾选要入仓的产品', 'status' => 2);
        }
        if (!$data['prolist'][0]['warehouse_id']) {
            return array('msg' => '请选择仓库', 'status' => 2);
        }
        if (!$data['prolist'][0]['place_id']) {
            return array('msg' => '请选择位置', 'status' => 2);
        }
        //使用一个foreach 判断数据
        foreach ($data['prolist'] as $datakeys => $datavals) {
            //判断货柜体积是否存在
            if ($datavals['sum_cbm'] <= 0) {
                return array('msg' => '该货柜体积为0，不能入库', 'status' => 2);
                exit();
            }
            $buy_po_pack = Db::name('buy_po_pack')->field('warehouse_id,place_id')->where('id', $datavals['po_pack_id'])->find();
            //清单已经入库了，就无法再进行入库
            if ($buy_po_pack['warehouse_id'] > 0) {
                return array('msg' => '该清单已经入库', 'status' => 2);
                exit();
            }
        }
        $po_ids = implode(',', array_unique(array_column($data['prolist'], 'po_id')));//入库的合同ids
        //计算这个产品的成本
        $BRL2CNY = getconfig()['BRL2CNY'];
        $cost = getconfig()['COST_CBM'];
        //计算每一个产品的的总的入库数量
        foreach ($data['prolist'] as $datakey => $dataval) {
            //计算cif
            cifpro($dataval, $po_ids, $data['container_id'], $BRL2CNY, $cost);
            $resinsert = $this->addstore($dataval);
            //更新装箱明细表
            $updatepro_pack['warehouse_id'] = $dataval['warehouse_id'];//仓库id
            $updatepro_pack['place_id'] = $dataval['place_id'];//位置id
            Db::name('buy_po_pack')->where('id', $dataval['po_pack_id'])->update($updatepro_pack);
        }
        //当这个货柜所有产品都入仓了，才可以更新时间
        $buypomap['container_id'] = $data['container_id'];
        $buypomap['warehouse_id'] = 0;
        $po_packdata = Db::name('buy_po_pack')->where($buypomap)->select();
        if (!$po_packdata) {
            $buy_data = $this->where('id', $data['container_id'])->setField('arrival_day', time());
        } else {
            $buy_data = 1;
        }
        if ($buy_data) {
            return array('msg' => lang("successfully_entered_the_warehouse"), 'status' => 1);
        } else {
            return array('msg' => lang("failed_to_enter_warehouse"), 'status' => 0);
        }
    }

    //入仓位置的写入
    protected function addstore($data)
    {
        //判断这个产品是否存在库存中
        $insert['pro_id'] = $mapthree['pro_id'] = $maptwo['pro_id'] = $mapone['pro_id'] = $data['pro_id'];//产品id
        $insert['prochar_id'] = $mapthree['prochar_id'] = $maptwo['prochar_id'] = $mapone['prochar_id'] = $data['prochar_id'];//角色id
        //无论是否填写位置都需要判断该产品是否已存在数据库
        $maptwo['place_id'] = $mapone['place_id'] = $mapone['warehouse_id'] = 0;
        $insert['qty'] = $qty = $data['qty'];//入库数量
        $infostoreone = Db::name('store')->where($mapone)->select();
        //更新产品的总库存
        if ($infostoreone) {
            //表示该产品有库存
            Db::name('store')->where($mapone)->setInc('qty', $qty);//更新总库存
        } else {
            //表示该产品没有库存
            $insert['warehouse_id'] = 0;
            $insert['place_id'] = 0;
            Db::name('store')->insert($insert);
        }
        //更新产品所在仓库数量
        $mapthree['warehouse_id'] = $maptwo['warehouse_id'] = $data['warehouse_id'];//仓库id
        $infostoretwo = Db::name('store')->where($maptwo)->select();
        if ($infostoretwo) {
            Db::name('store')->where($maptwo)->setInc('qty', $qty);
        } else {
            $insert['warehouse_id'] = $data['warehouse_id'];//仓库id
            $insert['place_id'] = 0;
            $data['record_id'] = Db::name('store')->insertGetId($insert);
            $this->storerecord($data);
        }
        if ($data['place_id'] > 0) {
            $mapthree['place_id'] = $data['place_id'];//位置id
            $infostorethree = Db::name('store')->where($mapthree)->select();
            if ($infostorethree) {
                //更新该位置产品的数量
                Db::name('store')->where($mapthree)->setInc('qty', $data['qty']);
            } else {
                $insert['warehouse_id'] = $data['warehouse_id'];//仓库id
                $insert['place_id'] = $data['place_id'];//位置id
                //该产品部不存在该位置
                $data['record_id'] = Db::name('store')->insertGetId($insert);//warehouse_id>0 place_id>0的数据写入
                $this->storerecord($data);
            }
        }
    }

    //取消入仓的操作
    public function setcancel($data)
    {
        $arrday = getValue('container', ['id' => $data['container_id']], 'arrival_day');
        if ($arrday) {
            if (time() - 172800 > $arrday) {
                return array('msg' => '入仓时间已超过2天，无法取消', 'status' => 0);
            }
        }
        $info_buy = getinfo($data['po_pack_id'], 'buy_po_pack', 'warehouse_id,place_id');
        $mapthree['pro_id'] = $maptwo['pro_id'] = $mapone['pro_id'] = $data['pro_id'];//产品id
        $mapthree['prochar_id'] = $maptwo['prochar_id'] = $mapone['prochar_id'] = $data['prochar_id'];//角色id
        $mapthree['warehouse_id'] = $mapone['warehouse_id'] = $info_buy['warehouse_id'];//仓库id
        $mapone['place_id'] = $info_buy['place_id'];//位置id
        //更新这个产品的库存数量
        $mapthree['place_id'] = $maptwo['place_id'] = $maptwo['warehouse_id'] = 0;
        //该位置产品数量
        $qtysum = getValue('store', $mapone, 'qty');
        if ($qtysum < $data['qty']) {
            //表示该位置下不够取消入仓
            return array('msg' => '该位置下产品数量不够取消', 'status' => 0);
        } else {
            //更新产品cif数据
            quxiaocifpro($data);
            //产品总库存减少
            $res = Db::name('store')->where($maptwo)->setDec('qty', $data['qty']);
            //该仓库产品数量
            Db::name('store')->where($mapthree)->setDec('qty', $data['qty']);
            //该位置下数量减少
            Db::name('store')->where($mapone)->setDec('qty', $data['qty']);
            //将入仓时间清空
            $this->where('id', $data['container_id'])->setField('arrival_day', 0);
            //更新装箱明细表
            Db::name('buy_po_pack')->where('id', $data['po_pack_id'])->update(['warehouse_id' => 0, 'place_id' => 0]);
            //对qty=0 sale_qty=0的数据（删除处理）
            Db::name('store')->where(['qty' => 0, 'sale_qty' => 0])->delete();
            if ($res) {
                return array('msg' => lang("cancel_successfully_entered_the_warehouse"), 'status' => 1);
            } else {
                return array('msg' => lang("cancel_failed_to_enter_warehouse"), 'status' => 0);
            }
        }
    }

    protected function storerecord($datares)
    {
        Db::name('store_record')->strict(false)->insert($datares);
    }
}